const loadGruntTasks = require('load-grunt-tasks')
const sass = require('sass')

module.exports = grunt => {

  grunt.initConfig({
    // clean
    clean: {
      main: 'dist/**'
    },
    // style
    sass: {
      options: {
        implementation: sass,
        // sourceMap: true,
      },
      main: {
        files: [
          {
            expand: true,
            cwd: 'src/assets/styles/',
            src: ['**/*.scss'],
            dest: 'dist/assets/styles',
            ext: '.css'
          }
        ]
      }
    },
    // script
    babel: {
      options: {
        presets: ['@babel/preset-env']
      },
      main: {
        files: [
          {
            expand: true,
            cwd: 'src/assets/scripts/',
            src: ['**/*.js'],
            dest: 'dist/assets/scripts',
            ext: '.js'
          }
        ]
      }
    },
    // html
    swigtemplates: {
      options: {
        defaultContext: {
          pkg: require('./package.json')
        },
        templatesDir: 'src'
      },
      main: {
        src: ['src/*.html'],
        dest: 'dist/'
      }
    },
    // 图片和字体压缩
    imagemin: {
      img: {
        files: [
          {
            expand: true,
            cwd: 'src/assets/images/',
            src: ['**/*'],
            dest: 'dist/assets/images'
          }
        ]
      },
      font: {
        files: [
          {
            expand: true,
            cwd: 'src/assets/fonts/',
            src: ['**/*'],
            dest: 'dist/assets/fonts'
          }
        ]
      }
    },
    // 拷贝公共文件
    copy: {
      main: {
        files: [
          {
            expand: true,
            cwd: 'public/',
            src: ['*'],
            dest: 'dist'
          }
        ]
      }
    },
    // 依赖文件合并
    concat: {
      options: {
        separator: ';',
        stripBanners: true // 去掉代码中的注释块
      },
      js: {
        src: [
          "./node_modules/jquery/dist/jquery.js",
          "./node_modules/popper.js/dist/umd/popper.js",
          "./node_modules/bootstrap/dist/js/bootstrap.js",
        ], // 需要构建的js路径  
        dest: "dist/assets/scripts/vendor.js" // 构建后js路径 
      },
      css:{
        src: [
          "./node_modules/bootstrap/dist/css/bootstrap.css"
        ],
        dest: "dist/assets/styles/vendor.css"
      }
    },
    // 处理html文件注释
    useref: {
      html: 'dist/*.html',
      temp: 'dist'
    },
    //压缩CSS
    cssmin: {
      main: {
        options: {
          report: 'min',
          // sourceMap: true
        },
        files: [
          {
            expand: true,
            cwd: 'dist/assets/styles',
            src: ['**/*.css'],
            dest: 'dist/assets/styles'
          }
        ]
      }
    },
    // 压缩js
    uglify: {
      main: {
        options: {
          // sourceMap: true
        },
        files: [{
            expand: true,
            cwd: 'dist/assets/scripts',
            src: ['**/*.js'],
            dest: 'dist/assets/scripts'
        }]
      }
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      }
    },
    // serve
    connect: {
      serve: {
        options: {
          port: 3333,
          hostname: '*',
          base: ['dist', 'node_modules'],
          keepalive: true
        }
      }
    }
  })

  loadGruntTasks(grunt)
  grunt.registerTask('compile', ['clean', 'sass', 'babel', 'swigtemplates', 'imagemin', 'copy', 'concat', 'useref', 'watch'])
}